<template>
  <div class="app-container" id='goodsIndex'>
    <div class="filter-container" style="float: right;">
      <el-input v-model="listQuery.goodsSn" clearable class="filter-item" style="width: 200px;" placeholder="请输入商品编号"></el-input>
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入商品名称"></el-input>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" border fit highlight-current-row>
      <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="table-expand">
              <el-form-item label="宣传画廊">
                <img v-for="pic in props.row.gallery" :key="pic" :src="pic" class="gallery">
              </el-form-item>
              <el-form-item label="商品介绍">
                <span>{{ props.row.brief }}</span>
              </el-form-item>
              <el-form-item label="商品单位">
                <span>{{ props.row.unit }}</span>
              </el-form-item>
              <el-form-item label="关键字">
                <span>{{ props.row.keywords }}</span>
              </el-form-item>
              <el-form-item label="类目Id">
                <span>{{ props.row.categoryId }}</span>
              </el-form-item>
              <el-form-item label="品牌商Id">
                <span>{{ props.row.brandId }}</span>
              </el-form-item>
            </el-form>
          </template>
      </el-table-column>
      <el-table-column prop="goodsSn" align="center" label="商品编号" sortable width="120">
      </el-table-column>
      <el-table-column prop="name" align="center" label="名称" width="200" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="picUrl" align="center" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="40">
        </template>
      </el-table-column>
      <el-table-column prop="shareUrl" align="center" label="分享图">
          <template slot-scope="scope">
            <img :src="scope.row.shareUrl" width="40">
          </template>
        </el-table-column>
      <el-table-column align="center" label="详情" prop="detail">
          <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="showContent(scope.row.detail)">查看</el-button>
          </template>
      </el-table-column>
      <el-table-column prop="counterPrice" align="center" label="专柜价价">
      </el-table-column>
      <el-table-column prop="retailPrice" align="center" label="当前价格">
      </el-table-column>
      <el-table-column prop="isNew" align="center" label="是否新品">
          <template slot-scope="scope">
              <el-tag :type="scope.row.isNew ?'success':'error'">{{scope.row.isNew ?'新品':'非新品'}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column prop="isHot" align="center" label="是否热卖">
          <template slot-scope="scope">
              <el-tag :type="scope.row.isHot ?'success':'error'">{{scope.row.isHot ?'热卖':'非热卖'}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column prop="isOnSale" align="center" label="是否在售">
          <template slot-scope="scope">
              <el-tag :type="scope.row.isOnSale ?'success':'error'">{{scope.row.isOnSale ?'在售':'非在售'}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :visible.sync="contentDialogVisible" title="专题详情" width="55%">
      <div v-html="contentDetail"/>
    </el-dialog>
  </div>
</template>
<script>
  import Pagination from '@/components/Pagination'
  import {
    listGoods,
    deleteGoods,
  } from '@/api/goods/goods'
  export default {
    name: 'goodsIndex',
    components: {
      Pagination
    },
    computed: {
    },
    data() {
      return {
        //查询数据
        listQuery: {
          page: 1,
          limit: 10,
          goodsSn: '',
          name: '',
        },
        //总条数
        total: 0,
        //表格数据
        tableData: [],
        //添加或修改模态框状态
        dialogFromVisible: false,
        //打开详情模态框状态
        contentDialogVisible: false,
        //存储详情数据
        contentDetail: '',
      }
    },
    created() {
      // 数据初始化
      this.getList();
    },
    methods: {
      //查询列表
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      //获取列表数据接口
      getList() {
        listGoods(this.listQuery).then(res => {
          this.tableData = [];
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
      },
      //进入新增页面
      handleCreate() {
      this.$router.push({path:'/goods/addgoods'})
      },
      //进入修改页面
      handleUpdate(row) {
        this.$router.push({path:'/goods/editgoods',query:{id:row.id}})
      },
      //删除
      handleDelete(row) {
        this.$confirm('此操作将删除该专题，是否继续？','提示',{
          confirmButtonText: '确定',
          concelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteGoods(row).then(res => {
            this.getList();
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
          })
        }).catch(() => {
           this.$message({
             type: 'info',
             message: '已取消删除'
           })
        })
      },
      //查看详情
      showContent(content){
         this.contentDetail = content;
         this.contentDialogVisible = true;
      },
    }
  }
</script>
<style>
  .table-expand label{
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item{
    width: 100%;
    margin-bottom: 0px;
  }
  .gallery{
      width: 80px;
      margin-right: 10px;
  }

</style>
<style scoped>
</style>
